package com.cljz.makepassenger.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.present.activity.MessageSettingP;
import com.cljz.makepassenger.utils.T;
import com.cljz.makepassenger.widget.EaseSwitchButton;
import com.cljz.makepassenger.widget.SettingNotifyDialog;
import com.jakewharton.rxbinding.view.RxView;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.router.Router;
import cn.jpush.android.api.JPushInterface;
import rx.functions.Action1;

/**
 * Created by Administrator on 2017/4/7 0007.
 */

public class MessageSettingActivity extends BaseActivity<MessageSettingP> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.ease_switch)
    EaseSwitchButton easeSwitch;
    private static boolean isOpen = true;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.activity_message_setting)
    LinearLayout activityMessageSetting;


    @Override
    public void initData(Bundle savedInstanceState) {
        tvTitleName.setText("消息设置");
        if (isOpen) {
            easeSwitch.openSwitch();
        } else {
            easeSwitch.closeSwitch();
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_message_setting;
    }

    @Override
    public MessageSettingP newP() {
        return new MessageSettingP();
    }

    @Override
    protected void initListener() {
        super.initListener();
        //点击返回箭头
        RxView.clicks(ivBack)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(1, TimeUnit.SECONDS).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                exit();
            }
        });
        //点击按钮
        RxView.clicks(easeSwitch)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                if (easeSwitch.isSwitchOpen()) {
                    SettingNotifyDialog notifyDialog = new SettingNotifyDialog(MessageSettingActivity.this, MessageSettingActivity.this, R.style.MyDialog, "MessageSettingActivity");
                    notifyDialog.show();
                    notifyDialog.mTxtDialogExit.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            easeSwitch.closeSwitch();
                            isOpen = false;
                            JPushInterface.stopPush(getApplicationContext());
                            T.getInstance().showShort("您已关闭消息推送通知！将不会收到推送消息。");
                            finish();
                        }
                    });
                } else {
                    easeSwitch.openSwitch();
                    isOpen = true;
                    JPushInterface.resumePush(getApplicationContext());
                    T.getInstance().showShort("您已开启消息推送通知！");
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        exit();
    }

    private void exit() {
        Router.newIntent(MessageSettingActivity.this).to(MainActivity.class).putBoolean("showDrawer",true).launch();
        Router.pop(MessageSettingActivity.this);
    }

}
